package com.OnTimeTeam.OnTime;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class DefaultsIO{
	
	
	public static SmartAlarmFeatures getDefaults(String filePath) {

		SmartAlarmFeatures af = new SmartAlarmFeatures();
		
		try {
			

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File(filePath));

            Element root = doc.getDocumentElement(); //ROOT
            NodeList features = root.getChildNodes(); //FEATURES LIST
            
            af.setArrivalHour(features.item(1).getTextContent());
            af.setArrivalMinute(features.item(3).getTextContent());
            af.setHour(features.item(5).getTextContent());
            af.setMinute(features.item(7).getTextContent());
            af.setDaysToRepeat(features.item(9).getTextContent());
            af.setLabel(features.item(11).getTextContent());
            af.setSnoozeInterval(features.item(13).getTextContent());
            af.setSnoozeRepeat(features.item(15).getTextContent());
            af.setTune(features.item(17).getTextContent());
            af.setVolume(features.item(19).getTextContent());
            af.setVibration(features.item(21).getTextContent());
            af.setDest(features.item(23).getTextContent());
            af.setOrigin(features.item(25).getTextContent());
            af.setTTO(features.item(27).getTextContent());
            af.setTransport(features.item(29).getTextContent());



        }catch (Exception err) {//Catch any Exception
        	err.printStackTrace();
        }
		
		return af;//RETURN RESULT
	}
	

	public static void setDefaults(String filePath, SmartAlarmFeatures af) {
		
		deleteFile(filePath);
		
		try {

    		//create file object in the path given
    		File file = new File(filePath);
    		if(file.createNewFile()) {
		        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	        	DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
	            Document doc = docBuilder.newDocument();
	            doc.normalize();
	

	            //create the root element named addresses and add it to the document
	            Element root = doc.createElement("defaults");
	            doc.appendChild(root);
	

	            Element arrivalHour = doc.createElement("arrivalHour");
	            Element arrivalMinute = doc.createElement("arrivalMinute");
	            Element hour = doc.createElement("hour");
	            Element minute = doc.createElement("minute");
	            Element repeat = doc.createElement("repeat");
	            Element label = doc.createElement("label");
	            Element snoozeIntrval = doc.createElement("snoozeIntrval");
	            Element snoozeRepeat = doc.createElement("snoozeRepeat");
	            Element tune = doc.createElement("tune");
	            Element volume = doc.createElement("volume");
	            Element vibration = doc.createElement("vibration");
	            Element dest = doc.createElement("dest");
	            Element origin = doc.createElement("origin");
	            Element tto = doc.createElement("tto");
	            Element transport = doc.createElement("transport");
	            
	            
	            if(af != null) {
		            arrivalHour.setTextContent(af.getArrivalHour());
		            arrivalMinute.setTextContent(af.getArrivalMinute());
		            hour.setTextContent(af.getHour());
		            minute.setTextContent(af.getMinute());
		            repeat.setTextContent(af.getDaysToRepeat());
		            label.setTextContent(af.getLabel());
		            snoozeIntrval.setTextContent(af.getSnoozeInterval());
		            snoozeRepeat.setTextContent(af.getSnoozeRepeat());
		            tune.setTextContent(af.getTune());
		            volume.setTextContent(af.getVolume());
		            vibration.setTextContent(af.getVibration());
		            dest.setTextContent(af.getDest());
		            origin.setTextContent(af.getOrigin());
		            tto.setTextContent(af.getTTO());
		            transport.setTextContent(af.getTransport());
	            }
	            else {
		            arrivalHour.setTextContent("10");
		            arrivalMinute.setTextContent("00");
		            
		            hour.setTextContent("");
		            minute.setTextContent("");
		            
		            repeat.setTextContent("");
		            label.setTextContent("");
		            snoozeIntrval.setTextContent("10");
		            snoozeRepeat.setTextContent("5");
		            tune.setTextContent("0");
		            volume.setTextContent("100");
		            vibration.setTextContent("Standard I");
		            dest.setTextContent("");
		            origin.setTextContent("");
		            tto.setTextContent("10");
		            transport.setTextContent("Vehicle");
	            }
	            
	            root.appendChild(arrivalHour);
	            root.appendChild(arrivalMinute);
	            root.appendChild(hour);
	            root.appendChild(minute);
	            root.appendChild(repeat);
	            root.appendChild(label);
	            root.appendChild(snoozeIntrval);
	            root.appendChild(snoozeRepeat);
	            root.appendChild(tune);	
	            root.appendChild(volume);
	            root.appendChild(vibration);
	            root.appendChild(dest);
	            root.appendChild(origin);
	            root.appendChild(tto);
	            root.appendChild(transport);
	            
	            /////////////////
	            //Output the XML
	
	            //set up a transformer
	            TransformerFactory transfac = TransformerFactory.newInstance();
	            Transformer trans = transfac.newTransformer();
	            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
	            trans.setOutputProperty(OutputKeys.INDENT, "yes");
	
	            //create string from xml tree
	            StringWriter sw = new StringWriter();
	            StreamResult result = new StreamResult(sw);
	            DOMSource source = new DOMSource(doc);
	            trans.transform(source, result);
	            String xmlString = sw.toString();
	    		
	            //Finally - write to the file the node "reminders"
				FileWriter fStream = new FileWriter(filePath);
				BufferedWriter out = new BufferedWriter(fStream);
	    		out.write(xmlString);
	    		
	    		//close the file
	    		out.close();
    		}
	    		
		}//end of try block
		catch (Exception e) {//catch any exception
		    System.out.println(e);
		}
		
	}
	

	private static void deleteFile(String filePath) {
		File file = new File(filePath);
		file.delete();
	}

}
